<template>
  <el-card class="box-card">
    <div slot="header" class="clearfix">
      <span>重置密码</span>
    </div>
    <!-- 表单 -->
    <el-form
      :model="pwdForm"
      :rules="pwdFormRules"
      ref="pwdFormRef"
      label-width="100px"
    >
      <el-form-item label="原密码" prop="old_pwd">
        <el-input v-model="pwdForm.old_pwd" type="password"></el-input>
      </el-form-item>
      <el-form-item label="新密码" prop="new_pwd">
        <el-input v-model="pwdForm.new_pwd" type="password"></el-input>
      </el-form-item>
      <el-form-item label="确认新密码" prop="re_pwd">
        <el-input v-model="pwdForm.re_pwd" type="password"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button @click="submitForm('pwdFormRef')" type="primary">修改密码</el-button>
        <el-button @click="resetForm('pwdFormRef')">重置</el-button>
      </el-form-item>
    </el-form>
  </el-card>
</template>

<script>
export default {
  name: 'UserPwd',
  data () {
    var validatePass1 = (rule, value, callback) => {
      // rule:规则对象
      // val对应当前校验规则对应的数据项：old_pwd
      if (value === '') {
        callback(new Error('请再次输入新密码'))
      } else if (value === this.pwdForm.old_pwd) {
        callback(new Error('不能和旧密码一致!'))
      } else {
        callback()
      }
    }
    var validatePass2 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入新密码'))
      } else if (value !== this.pwdForm.new_pwd) {
        callback(new Error('两次输入密码不一致!'))
      } else {
        callback()
      }
    }
    return {
      // 表单的数据对象
      pwdForm: {
        old_pwd: '',
        new_pwd: '',
        re_pwd: ''
      },
      // 表单的验证规则对象
      pwdFormRules: {
        old_pwd: [{ required: true, message: '请输入密码', trigger: 'blur' },
          { pattern: /^\S{6,15}$/, message: '必须是6-15位非空字符', trigger: 'blur' }],
        new_pwd: [{ required: true, message: '请输入密码', trigger: 'blur' },
          { validator: validatePass1, trigger: 'blur' },
          { pattern: /^\S{6,15}$/, message: '必须是6-15位非空字符', trigger: 'blur' }],
        re_pwd: [{ required: true, message: '请再次输入密码', trigger: 'blur' },
          { validator: validatePass2, trigger: 'blur' }, { pattern: /^\S{6,15}$/, message: '必须是6-15位非空字符', trigger: 'blur' }]
      }
    }
  },
  methods: {
    submitForm (formName) {
      // validate对整个表单进行校验
      this.$refs[formName].validate(async valid => {
        if (valid) {
          // 参数一：url，参数二:要传递的数据对象
          // console.log(this.pwdForm)
          const res = await this.$http.patch('/my/updatepwd', this.pwdForm)
          // console.log(res)
          if (res.data.code === 1) {
            alert(res.data.message)
          } else {
            this.$message({ type: 'success', message: res.data.message })
          }
          // this.$refs[formName].resetFields()
        } else {
          this.$message({ type: 'error', message: '修改密码失败' })
        }
      })
    },
    resetForm (formName) {
      this.$refs[formName].resetFields()
    }
  }
}
</script>

<style lang="less" scoped>
.el-form {
  width: 500px;
}
</style>
